<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '240px'" class="layout-aside">
      <div class="aside-header">
        <div class="logo" :class="{ 'is-collapse': isCollapse }">
          <el-icon size="24" class="logo-icon">
            <ElementPlus />
          </el-icon>
          <span v-show="!isCollapse" class="logo-text">管理后台</span>
        </div>
      </div>

      <!-- 侧边栏菜单 -->
      <SidebarMenu :is-collapse="isCollapse" />
    </el-aside>

    <!-- 主体区域 -->
    <el-container class="main-container">
      <!-- 顶部导航栏 -->
      <el-header class="layout-header">
        <div class="header-left">
          <el-button :icon="isCollapse ? Expand : Fold" text class="collapse-btn" @click="toggleCollapse" />
          <Breadcrumb />
        </div>

        <div class="header-right">
          <HeaderActions />
        </div>
      </el-header>

      <!-- 主内容区 -->
      <el-main class="layout-main">
        <div v-loading="appStore.loading" class="main-content">
          <router-view v-slot="{ Component, route }">
            <transition name="fade-transform" mode="out-in">
              <keep-alive :include="cachedViews">
                <component :is="Component" :key="route.path" />
              </keep-alive>
            </transition>
          </router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { useAppStore } from '@/stores'
import SidebarMenu from './components/SidebarMenu.vue'
import Breadcrumb from './components/Breadcrumb.vue'
import HeaderActions from './components/HeaderActions.vue'
import { Expand, Fold } from '@element-plus/icons-vue'

const appStore = useAppStore()

// 侧边栏折叠状态
const isCollapse = ref(false)

// 缓存的视图组件
const cachedViews = computed(() => appStore.cachedViews)

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 监听窗口大小变化，自动折叠侧边栏
const handleResize = () => {
  if (window.innerWidth < 768) {
    isCollapse.value = true
  }
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
  handleResize()
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.layout-aside {
  background: var(--el-bg-color);
  border-right: 1px solid var(--el-border-color);
  transition: width 0.3s;
  overflow: hidden;
}

.aside-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--el-border-color);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: bold;
  color: var(--el-color-primary);
  transition: all 0.3s;
}

.logo.is-collapse {
  gap: 0;
}

.logo-icon {
  flex-shrink: 0;
}

.logo-text {
  white-space: nowrap;
  transition: opacity 0.3s;
}

.main-container {
  flex: 1;
  overflow: hidden;
}

.layout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color);
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.collapse-btn {
  font-size: 18px;
}

.layout-main {
  padding: 0;
  background: var(--el-bg-color-page);
  overflow-y: auto;
}

.main-content {
  padding: 16px;
  min-height: calc(100vh - 120px);
}

/* 页面切换动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.3s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(-30px);
}

/* 响应式 */
@media (max-width: 768px) {
  .layout-aside {
    position: fixed;
    z-index: 1000;
    height: 100vh;
  }

  .main-container {
    margin-left: 0;
  }
}
</style>
